package com.admin.themechange

import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import android.support.v7.app.AppCompatActivity
import android.util.TypedValue
import java.util.*

/**
 * author 吴昶
 * date 2019/5/29
 * 标注
 */
abstract class BaseActivity:AppCompatActivity(){


    override fun onCreate(savedInstanceState: Bundle?) {
//        setStyle()
        super.onCreate(savedInstanceState)
        setContentView(bindLayout())
        bindEvent()
    }

    abstract fun bindLayout():Int

    abstract fun bindEvent()

    fun setLanguage(l:String){
        var metrics=resources.displayMetrics
        var config=resources.configuration
        if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.M) {
            config.locale= Locale.getDefault()
            if (l == "ch") {
                config.locale= Locale.SIMPLIFIED_CHINESE
            } else if (l == "en") {
                config.locale= Locale.ENGLISH
            }
        }else{
            var localeList=config.locales
            if (l == "ch") {
                localeList= LocaleList(Locale.SIMPLIFIED_CHINESE)
            } else if (l == "en") {
                localeList= LocaleList(Locale.ENGLISH)
            }
            config.locales=localeList
        }
        resources.updateConfiguration(config, metrics)
    }

//    fun setStyle(){
//        val typedValue = TypedValue()
//        theme.resolveAttribute(R.attr.myTheme, typedValue, true)
//        when(SharedPreferencesUntil.getValue("style",0) as Int){
//            0->{
//                setTheme(R.style.DayTheme)
//            }
//            1->{
//                setTheme(R.style.NightTheme)
//            }
//        }
//    }
}